package com.wushi.lenovo.asia5b.app_push;

import android.app.Application;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;
import com.mvp.helper.AbsCrashHelper;
import com.wushi.lenovo.asia5b.app_config.MalayAsiaApplication;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/12/19.
 * 封装一些与firebase推送的功能
 * todo 通过不同google-services.json配置线上,线下环境
 */
public class FireBasePushHelper extends PushHelper {
    public static final boolean isOpenFireBase = true;////2.2.0/2.2.1版本暂不发行firebase

    public static final String channel_data = "channel_data";
    public static final int channel_data_value = 888;


    /**
     * todo 初始化FireBase推送,如果初始化失败,就初始化友盟推送
     */
    public static void initFireBase(Application application) {
        if (isOpenFireBase) {
            FirebaseApp.initializeApp(application);
            FirebaseInstanceId.getInstance().getInstanceId()
                    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                        @Override
                        public void onComplete(@NonNull Task<InstanceIdResult> task) {
                            if (!isOpenFireBase) {
                                return;
                            }
                            if (!task.isSuccessful()) {
                                AbsCrashHelper.postCatchedException(task.getException());
                                initUMPush(application);
                                return;
                            }

                            // Get new Instance ID token
                            String token = task.getResult().getToken();
                            if (TextUtils.isEmpty(token)) {
                                initUMPush(application);
                            } else {
                                PushHelper.setRegistrationIdAndType(token, "0");
                                d("firebase推送--->>>>>>>>---firebase注册成功deviceToken:" + token);

                                //给firebase推送建群
                                FireBasePushHelper.subscribe();
                            }

                        }
                    });
//        if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(application) < 0) {
//            UMengPushHelper.registUmengPush(application);
//        }
        } else {
            initUMPush(application);
        }
    }

    public static void initUMPush(Application application) {
        ((MalayAsiaApplication) application).initUMPush();
    }

    //检查是否有谷歌服务,没有就弹窗提示,todo 我们就不需要提示了
//        GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this);


    /**
     * 主题订阅(主要是为了区分线上/线下)
     */
    public static void subscribe() {
        FirebaseMessaging.getInstance().subscribeToTopic(FireBasePushHelper.getSubs1())
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (!task.isSuccessful()) {
                            d("--->>>>>>>>---订阅失败");
                        } else {
                            d("--->>>>>>>>---订阅成功");
                        }
                    }
                });
        FirebaseMessaging.getInstance().subscribeToTopic(FireBasePushHelper.getSubs2())
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (!task.isSuccessful()) {
                            d("--->>>>>>>>---订阅失败");
                        } else {
                            d("--->>>>>>>>---订阅成功");
                        }
                    }
                });
    }

    /**
     * 主题1
     *
     * @return
     */
    private static String getSubs1() {
        return "all_device";
    }

    /**
     * 主题2
     *
     * @return
     */
    private static String getSubs2() {
        return "all_android";
    }


}
